﻿<?php

class CityModel extends BaseModel
{

    var $table  = 'city_area';	

    var $_page_rows = 20;	
	
     function show()
     {  
	 
	 
 		 $info = $this->S_Sql_Exe(array('fields' =>'*','order' =>"id asc"));   
		 return $info;
        
     }

    /* 插入数据 */
    function post($data)
    {    
	
  		return $this->IU_Sql_Exe(array ('data' => array('name'=>$data["name"],'fup'=>$data["fup"]), 'mode' => 'INSERT'));

        
    }


    /* 通过id取出一条数据 */
    function oneshow($id ='')
    {    
	      
		 $info = $this->S_Sql_Exe(array('fields' =>'*','conditions' =>"id='{$id}'",'return' =>'one'));
    
		 return $info;
        
    }


    /* 修改数据 */
    function editaction($id ='',$data = '')
    {    
      
		return $this->IU_Sql_Exe(array ('data' => array('name'=>$data["name"],'fup'=>$data["fup"]),'mode' => 'UPDATE','conditions' =>"id = '{$id}' "));
        
    }


    /* 删除数据 */
    function del($id ='')
    {      
		return $this->D_Sql_Exe(array ('conditions' =>"id = '{$id}' "));
        
    }

	
      function getnbsp($i) 
	  {
	        $str = '';
	        if($i) 
			{
		         for($j=0; $j<$i; $j++)
				 {
			      $str .= "　";
		         }
	        }
	        return $str;
     }


      function write_cache() 
	  {

		$data = $this->get_list_category($this->show());
		  		 		  
		$cache_filename = ROOT_PATH . '/data/city/city_area.php';
		
	    write_file($cache_filename, "<?php \nreturn " . var_export($data , true) . ";\n?>");
		
		if(!is_writable($cache_filename))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
		
     }



     function get_list_category($array,$parentid=0,$level=0,$add=2) 
	 {
	        $str_pad = $this->getnbsp($level);
	        $newarray = array();
	        $temp = array();
	        foreach($array as $v) 
			{
		       if($v['fup'] == $parentid) 
			   {
			       $newarray[] = array(
				         'id' => $v['id'],
				         'name' => $v['name'],
				         'fup' => $v['fup'],
				         'str_pad' => $str_pad
			                          );
			       $temp = $this->get_list_category($array,$v['id'],($level+$add));
			       if($temp) 
				   {
				          $newarray = array_merge($newarray, $temp);
			       }
		      }
	       }
	       return $newarray;
    }


/*    function html_format_category($array,$value,$parentid=0,$level=0) 
	{
	       $str = '';
	       $newarray = $this->get_list_category($array,$parentid=0,$level=0,$add=1);

	       foreach($newarray as $v) 
		   {
		            $selected = '';
		            if($value == $v['id']) 
					{
					 $selected = 'selected';
					}
		            $str .= "<option value='".$v['id']."' ".$selected.">".$v['str_pad'].$v['name']."</option>";
	       }
	       return $str;
    }*/
    function html_format_category($array,$value,$parentid=0,$level=0,$ownid=0) 
	{
	       $newarray = $this->get_list_category($array,$parentid=0,$level=0,$add=1);
            $str="";
	       foreach($newarray as $v) 
		   {
		            $selected = '';
		            if($value == $v['id']) 
					{
					 $selected = 'selected';
					}
					if($ownid && $ownid == $v['id'])
			        {
					$str .= "<optgroup label='".$v['str_pad'].$v['name']."'></optgroup>";
					}else
			       {
		            $str .= "<option value='".$v['id']."' ".$selected.">".$v['str_pad'].$v['name']."</option>";
				   }
	       }
	       return $str;
    }


}




?>